package com.cjjd.c.view;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.widget.TextView;

/**
 * ----------------------------------
 * Created by xyd on 2017/11/20.
 * <p/>
 * 描  述：
 * <p/>
 * 修订历史:
 * ----------------------------------
 */

public class CountDownTextView extends TextView {
    public CountDownTextView(Context context) {
        super(context);
    }

    public CountDownTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public CountDownTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private int time;

    @Override
    public void setText(CharSequence text, BufferType type) {
        try {
            time = Integer.parseInt((String) text);
            int minute = time / 60;
            int secound = time % 60;

            StringBuffer showStr = new StringBuffer();

            if (time < 0) {
                //Log.e("xyd","value < 0  return");
                return;
            } else if (time == 0) {
                // 倒计时结束，调用结束的接口。
                if (getVisibility() == INVISIBLE || getVisibility() == GONE || mHandler != null) {
                    mHandler.removeMessages(sendAfterOneSecound);
                } else {
                    // ...
                }
                //Log.e("xyd","value is 0  remove message");
                onCountDownFinishedListener.onFinish();

            } else if (time > 0) {

                if (0 <= minute && minute < 10) {
                    showStr.append(0).append(minute);
                } else {
                    showStr.append(minute);
                }

                showStr.append(": ");

                if (0 <= secound && secound < 10) {
                    showStr.append(0).append(secound);
                } else {
                    showStr.append(secound);
                }

            }
            super.setText(showStr, type);
            if (getVisibility() == VISIBLE && mHandler != null) {
                mHandler.sendEmptyMessageDelayed(sendAfterOneSecound, 1000);
            }

        } catch (NumberFormatException e) {
            e.printStackTrace();
            super.setText(text, type);
        }
    }

    private final int sendAfterOneSecound = 0x00000086;
    private Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            if (msg.what == sendAfterOneSecound) {
                time--;
                setText("" + time);
            }

        }
    };

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        mHandler.removeMessages(sendAfterOneSecound);
    }

    private OnCountDownFinishedListener onCountDownFinishedListener;

    public void setOnCountDownFinishedListener(OnCountDownFinishedListener onCountDownFinishedListener) {
        this.onCountDownFinishedListener = onCountDownFinishedListener;
    }

    public interface OnCountDownFinishedListener {
        void onFinish();
    }
}
